package net.leieuncretino.beta.parole;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.google.ads.AdRequest.ErrorCode;

public class ParoleActivity extends Activity {
    
    private AdView ad = null;
    public static final boolean debug = false;
	
	protected static final String TAG = "ParoleActivity";

	
//** Commenta - blocco    
    
	private static final int DIALOG_NO_SAVED = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
		try {
       		super.onCreate(savedInstanceState);
       		
         	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

			startScreen();
		} catch (Exception e) {
			// Log.e(TAG,"top level",e);
		}
    }
    
//    @Override
//    public void onConfigurationChanged(Configuration newConfig) {
//    	// TODO Auto-generated method stub
//    	super.onConfigurationChanged(newConfig);
//    	ad = (AdView)findViewById(R.id.ad);
//    }

	private void startScreen() {
		setContentView(R.layout.start_screen);

		
        ad = (AdView)findViewById(R.id.ad);
        ad.setAdListener(new AdListener() {
			public void onReceiveAd(Ad arg0) {
				Log.d(ParoleActivity.this.TAG,"onReceiveAd " + arg0.toString() );
			}
			public void onPresentScreen(Ad arg0) {
				Log.d(ParoleActivity.this.TAG,"onPresentScreen " + arg0.toString() );
			}
			public void onLeaveApplication(Ad arg0) {
				Log.d(ParoleActivity.this.TAG,"onLeaveApplication " + arg0.toString() );
			}
			public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
				Log.d(ParoleActivity.this.TAG,"onFailedToReceiveAd " + arg1.toString() );
			}
			public void onDismissScreen(Ad arg0) {
				Log.d(ParoleActivity.this.TAG,"onDismissScreen " + arg0.toString() );
			}
		});
		
//*/
//*		
		
		Button b = (Button) findViewById(R.id.new_game);
		if (b != null) {
			b.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					startActivity(new Intent("net.leieuncretino.beta.parole.NEW_GAME"));
				}
			});
		}
		
		if(savedGame()) {
			b = (Button) findViewById(R.id.restore_game);
			if (b != null) {
				b.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						if(savedGame()) {
							startActivity(new 
								Intent("net.leieuncretino.beta.parole.RESTORE_GAME"));
						} else {
							showDialog(DIALOG_NO_SAVED);
						}
					}
				});
				b.setEnabled(true);
			}
		}

// TODO Fare Help con pagina html locale ?		
		b = (Button) findViewById(R.id.about);
		if (b != null) {
			b.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					startActivity(new Intent("net.leieuncretino.beta.parole.ABOUT"));
					
				}
			});
		}

		b = (Button) findViewById(R.id.preferences);
		if (b != null) {
			b.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					startActivity(new 
						Intent("net.leieuncretino.beta.parole.CONFIGURE"));
				}
			});
		}
		
//*/	
    
//** COmmenta - blocco    
	}

	public void onPause() {
		super.onPause();
    	if (ad != null) {
    		ad.setEnabled(false);
    	}
		
	}

	public void onResume() {
		Log.d(TAG, "SDK: " + android.os.Build.VERSION.SDK);
		super.onResume();
		startScreen();
    	if (ad != null) {
    		ad.setEnabled(true);
			AdRequest req = new AdRequest();
			if (debug) {
				req.setTesting(true);
				// req.addTestDevice(AdRequest.TEST_EMULATOR);
			}
			// Place keywords here
			req.addKeyword("best");
			req.addKeyword("android");
			req.addKeyword("game");
			req.addKeyword("word");
			req.addKeyword("puzzle");
			req.addKeyword("casual");
			
			ad.loadAd(req);
    	}
	}

	public boolean savedGame() {
		Resources res = getResources();
		SharedPreferences prefs = getSharedPreferences("prefs_game_file",
			MODE_PRIVATE);

		return prefs.getBoolean("activeGame",false);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id) {
			case DIALOG_NO_SAVED:
				return new AlertDialog.Builder(this)
					.setTitle(getResources().
						getString(R.string.dialog_no_saved))
					.setPositiveButton(R.string.dialog_ok, 
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, 
								int whichButton) {
									// do nothing.
								}
						})
					.create();
		}
		return null;
	}

    
//*/    
}